package com.example.bsxk.web;

import com.example.bsxk.dto.BaseResponse;
import com.example.bsxk.entity.Thesis;
import com.example.bsxk.service.LxinThesisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Controller
public class LxinThesisController {
    @Autowired
    LxinThesisService service;
//    @Autowired
//    UserService userService;
    @PostMapping("/theses")
    @ResponseBody
    public BaseResponse insert(Thesis thesis) {
        // 将课题信息 插入数据库
        // 浏览器传过来的参数:课题标题、内容、年份
        // 创建时间、更新时间、创建人、更新人、状态

        thesis.setCreated(new Date());
        thesis.setUpdated(new Date());

        thesis.setUid(1L);
        thesis.setUpdatedBy(1L);
        thesis.setCreatedBy(1L);
        thesis.setStatus("0");

        service.insert(thesis);

        return BaseResponse.success();
    }

    // /theses? id=1          @RequestParam("id")
    // /theses/1              @PathVariable("id")
    @DeleteMapping("/theses/{id}")
    @ResponseBody
    public BaseResponse delete(@PathVariable("id") Integer id) {

        System.out.println("id: " + id);

        service.delete(id);
        return BaseResponse.success();
    }

    @PutMapping("/theses/{id}")
    @ResponseBody
    public BaseResponse update(Thesis thesis, @PathVariable("id") Integer id) {

//      thesis1 = service.update();
//      System.out.println("id: " + id);

        thesis.setCreated(new Date());
        thesis.setUpdated(new Date());

        thesis.setUid(1L);
        thesis.setUpdatedBy(1L);
        thesis.setCreatedBy(1L);
        thesis.setStatus("0");

        service.update(thesis,id);
        return BaseResponse.success();
    }
}
